// pages/musicMain/musicMain.js
const common = require("../../common/js/common.js");
Page({
  data: {
    netEaseList: [],
    offset: 0
  },
  onLoad: function (options) {
    let that = this;
    loadSongListFun(that);
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {

  },
  onUnload: function () {

  },
  onPullDownRefresh: function () {
    console.log("执行——上拉刷新");
    let that = this;
    that.setData({
      offset: 0,
      netEaseList: []
    });
    loadSongListFun(that, () => {
      wx.stopPullDownRefresh();
    });
  },
  onReachBottom: function () {
    console.log("执行——下拉加载");
    let that = this;
    that.setData({
      offset: that.data.offset + 36
    });
    loadSongListFun(that);
  },
  onShareAppMessage: function () {

  },
  pageChangeFun(e) {
    this.setData({
      mainPageCurrent: e.detail.current
    });
  },
  cardChange(e) {
    this.setData({
      mainPageCurrent: e.target.dataset.cardind
    });
  },
  songListInfoFun(e) {
    wx.navigateTo({
      url: '../singListDetail/singListDetail?id=' + e.currentTarget.dataset.id,
      success: function (res) {
        // success
      },
      fail: function () {
        // fail
      },
      complete: function () {
        // complete
      }
    })
  }
})
function loadSongListFun(that, fn) {
  common.customLoading();
  let httpTask = common.http({
    url: common.baseUrl + '/top/playlist',
    query:{
      offset:that.data.offset,
      limit:36
    },
    success(data) {
      console.log(data);
      let newLoadData = that.data.netEaseList;
      data.playlists.forEach(element => {
        newLoadData.push(element);
      });
      that.setData({
        netEaseList: newLoadData
      });
    },
    fail(err) {
      common.coustomToast({
        title:err
      });
    },
    complete() {
      wx.hideLoading();
      if (fn) {
        fn();
      }
    }
  });
}